﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Security;



namespace TruckingService_WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        SQL2008R2_920039_cognitiveg2Entities1 entities = new SQL2008R2_920039_cognitiveg2Entities1();
        private string userEmail { get; set; }
        
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        public string GetCompanies()
        {
            var companies = entities.COMPANies.ToList();
            var result = "All My Companies -- ";
            foreach (var company in companies)
            {
                result += company.companyName + " , ";
            }
            
            return result;
        }

        public string Login(string username, string password)
        {
            if (Membership.ValidateUser(username, password))
            {
                userEmail = Membership.GetUser(username).Email;
                return userEmail;
            }
            return "fail";
        }

        public List<List<string>> GetOrders(int companyId)
        {
            var outterList = new List<List<string>>();

            var orders = entities.ORDERs.Where(o => o.companyId == companyId).ToList();

            foreach (var order in orders)
            {
                var thisOrder = new List<string>();
                thisOrder.Add(order.orderID.ToString());
                thisOrder.Add(order.orderNum.ToString());
                thisOrder.Add(order.gross.ToString());
                thisOrder.Add(order.net.ToString());
                thisOrder.Add(order.carrierRate.ToString());
                thisOrder.Add(order.max.ToString());
                thisOrder.Add(order.paidStatus.ToString());
                thisOrder.Add(order.dueDate.ToString());
                thisOrder.Add(order.mileage.ToString());
                thisOrder.Add(order.deadHeadMileage.ToString());
                thisOrder.Add(order.quickPay.ToString());
                thisOrder.Add(order.lumperFee.ToString());
                thisOrder.Add(order.brokerID.ToString());
                thisOrder.Add(order.carrierID.ToString());
                thisOrder.Add(order.orderID.ToString());
                thisOrder.Add(order.pickupCity);
                thisOrder.Add(order.pickupState);
                thisOrder.Add(order.dropOffCity);
                thisOrder.Add(order.dropOffState);
                thisOrder.Add(order.ORDER_STATUS.status);
                thisOrder.Add(order.notes);
                outterList.Add(thisOrder);
            }

            return outterList;
        }

        public Dictionary<string, string> AccountInformation(string email)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            var thisResource = entities.RESOURCEs.Where(r => r.email == userEmail).SingleOrDefault();
            result.Add("Email", thisResource.email);
            result.Add("Name", thisResource.FullName);

            var thisCompany = entities.RESOURCE_COMPANY.Where(r => r.resourceID == thisResource.resourceID).SingleOrDefault();
            result.Add("Company", thisCompany.companyID.ToString());
            
            return result;
        }

        public string Test()
        {
            return "ok";
        }
    }
}
